|
STEP 1: The Numbered File approach
|
The numbered-file approach uses JavaScript to generate a random number in a range determined by the number of files you'd like randomized. For example, if you have 15 music files that you'd like randomized, name the files something1.rmf, something2.rmf, something3.rmf,........something15.rmf. Then place these files in the same folder along with the HTML file that is to play them. The JavaScript will then generate a random number between 1 and 15 and create a filename using this number.
The JavaScript for this approach is shown in the source code listing below. Place it towards the end of your HTML file, in the spot where you'd like the random music file to be embedded. There are three variables you can use to customize the randomisation code: totalFiles, filesPrefix, and filesSuffix. They are highlighted in the copy-and-paste code listing.
The number of music files you have to randomize. Number your files starting at 1.
The part of the filename that appears before the number, and is the same in all the files you wish to randomize. For example, if you named the files "something1.rmf", "something2.rmf", etc., you would specify
You can also use the filesPrefix variable to specify a complete directory path to your files. For instance, if all the "something" files were buried in a deeper folder than the folder containing the HTML document in which you wish to place the random file, (eg. "music/random-pick/"), you would specify the path to the directory with the statement
The part of the filename that follows the number and is the same in all the files you wish to randomize. In most cases this will be just the file extension for your set of files. For example ".rmf" for RMF files, ".mid" for MIDI files, ".aiff" for AIFF files, ".wav" for WAV files, or ".au" for AU files. However, as is the case with the filesPrefix variable, you can use the filesSuffix variable to specify any amount of the tail end of the file name that is common to all of the files you are randomizing. For instance, if your files were named "something1-mellow.rmf", "something2-mellow.rmf", etc. then you could use the statement
| |
<SCRIPT SRC="music-object.js"></SCRIPT> <SCRIPT LANGUAGE=JavaScript><!-- // totalFiles = 15; filesPrefix = "your-files-prefix"; filesSuffix = "your-files-suffix"; Music.magicEmbed ( 'SRC="' + filesPrefix + (Math.round (Math.random () * (totalFiles - 1)) + 1) + filesSuffix + '" ' + 'TYPE="audio/rmf" ' + 'AUTOSTART=TRUE ' + 'WIDTH=0 ' + 'HEIGHT=0 ' + 'VOLUME=100 ' + 'LOOP=TRUE' ); // --></SCRIPT> |
Notice how we are using the Music.magicEmbed function, which is defined in the "music-object.js" JavaScript file that is sourced into the HTML document. We could have just used a document.writeln statement instead, but Music Object's Music.magicEmbed function performs some automatic compatibility testing to see if the user has the Beatnik Player installed. The Music.magicEmbed function also writes the correct EMBED tag or OBJECT tag syntax into the HTML document, depending on whether the user is browsing with Netscape Navigator or Microsoft Internet Explorer.